<template>
  <div class="upload-container">
    <span v-if="isProduct" style="color: red" v-html="tips"></span>
    <el-button :style="{background:color,borderColor:color}" icon="el-icon-upload" type="primary"
      @click=" dialogVisible=true">
      上传本地图片
    </el-button>
    <el-button v-if="isVideo" icon="el-icon-upload" type="warning"
      @click=" videoDialogVisible=true">
      上传本地视频
    </el-button>
    <el-dialog :visible.sync="dialogVisible">
      <el-upload :multiple="true" :file-list="fileList" :show-file-list="true" :on-remove="handleRemove"
        :on-success="handleSuccess" :before-upload="beforeUpload" class="editor-slide-upload" :action="action"
        :headers="headers" list-type="picture-card">
        <el-button size="small" type="primary">
          点击上传
        </el-button>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="handleSubmit">
          确定
        </el-button>
      </div>
    </el-dialog>

    <el-dialog title="上传视频" v-if="videoDialogVisible" append-to-body 
    :visible.sync="videoDialogVisible" width="600px">
			<el-upload
        class="upload-demo"
        ref="upload"
        :action="action"
        :on-success="handleVideoSuccess"
        :limit="1"
        :auto-upload="true">
        <el-button slot="trigger" size="small" type="primary">选取视频</el-button>
        <div slot="tip" class="el-upload__tip">每次上传一个，等到出现绿色勾号才为上传成功</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button @click="videoDialogVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="uploadVideo()">
          确定
        </el-button>
      </div>
		</el-dialog>
  </div>
</template>

<script>
// import { getToken } from 'api/qiniu'

import { BASEURL } from '../../../../public/static/config'
export default {
  name: 'EditorSlideUpload',
  props: {
    isProduct: {
      type: Boolean,
      default: true
    },
    isVideo: {
      type: Boolean,
      default: false
    },
    color: {
      type: String,
      default: '#1890ff'
    }
  },
  data() {
    return {
      action: (process.env.BASE_URL || BASEURL) + '/common/oss/single',
      headers: {
        token: this.$store.state.token
      },
      dialogVisible: false,
      listObj: {},
      fileList: [],
      tips: "提示：请上传产品细节图、预包装展示“标签标示内容”图&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;",
      videoDialogVisible: false,
      videoResultUrl: '',
    }
  },
  methods: {
    checkAllSuccess() {
      return Object.keys(this.listObj).every(item => this.listObj[item].hasSuccess)
    },
    handleSubmit() {
      const arr = Object.keys(this.listObj).map(v => this.listObj[v])
      if (!this.checkAllSuccess()) {
        this.$message('请等待所有图片上传成功。如果出现网络问题，请刷新页面重新上传!')
        return
      }
      this.$emit('successCBK', arr)
      this.listObj = {}
      this.fileList = []
      this.dialogVisible = false
    },
    handleSuccess(response, file) {
      const uid = file.uid
      const objKeyArr = Object.keys(this.listObj)
      for (let i = 0, len = objKeyArr.length; i < len; i++) {
        if (this.listObj[objKeyArr[i]].uid === uid) {
          this.listObj[objKeyArr[i]].url = response.result
          this.listObj[objKeyArr[i]].hasSuccess = true
          return
        }
      }
    },
    handleRemove(file) {
      const uid = file.uid
      const objKeyArr = Object.keys(this.listObj)
      for (let i = 0, len = objKeyArr.length; i < len; i++) {
        if (this.listObj[objKeyArr[i]].uid === uid) {
          delete this.listObj[objKeyArr[i]]
          return
        }
      }
    },
    beforeUpload(file) {
      const _self = this
      const _URL = window.URL || window.webkitURL
      const fileName = file.uid
      this.listObj[fileName] = {}
      return new Promise((resolve, reject) => {
        const img = new Image()
        img.src = _URL.createObjectURL(file)
        img.onload = function () {
          _self.listObj[fileName] = { hasSuccess: false, uid: file.uid, width: this.width, height: this.height }
        }
        resolve(true)
      })
    },
    handleVideoSuccess(res, file) {
      if (file.status == 'success') {
        this.videoResultUrl = file.response.result
      }
    },
    uploadVideo() {
      this.$emit('uploadVideoSuccess', this.videoResultUrl)
      this.videoResultUrl = ''
      this.videoDialogVisible = false
    }
  }
}
</script>

<style lang="scss" scoped>
.editor-slide-upload {
  margin-bottom: 20px;
  ::v-deep .el-upload--picture-card {
    width: 100%;
  }
}
</style>
